package com.lejin.usercenter.service.impl;


import cn.hutool.json.JSONUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lejin.usercenter.entity.UserBaseinfo;
import com.lejin.usercenter.mapper.UserBaseinfoMapper;
import com.lejin.usercenter.service.IUserBaseinfoService;
import com.lejin.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author luminghua
 * @since 2022-04-20
 */
@Service
public class UserBaseinfoServiceImpl extends ServiceImpl<UserBaseinfoMapper, UserBaseinfo> implements IUserBaseinfoService {
    @Autowired
    private UserBaseinfoMapper userBaseinfoMapper;



    @Override
    public ResultVO addUserInfo(UserBaseinfo userBaseinfo) {
        if (StringUtils.isEmpty("userBaseinfo")){


            return new ResultVO(false,"用户信息不能为空");
        }


        UserBaseinfo userBaseinfo1 = new LambdaQueryChainWrapper<UserBaseinfo>(userBaseinfoMapper).eq(UserBaseinfo::getUserId, userBaseinfo.getUserId()).one();
        if (StringUtils.isEmpty(JSONUtil.toJsonStr(userBaseinfo1))){
            int insert = userBaseinfoMapper.insert(userBaseinfo);
            if (insert>0){

                return new ResultVO(true,"添加成功!");

            }
        }
        UpdateWrapper<UserBaseinfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", userBaseinfo.getUserId());

        int update = userBaseinfoMapper.update(userBaseinfo, updateWrapper);
        if (update>0){
            return new ResultVO(true,"修改成功");
        }

        return new ResultVO(false,"修改失败！");
    }

    @Override
    public ResultVO findUserInfo(String userId) {
        if (StringUtils.isEmpty(userId)){


            return new ResultVO(false,"用户id为空");
        }




        UserBaseinfo userBaseinfo = new LambdaQueryChainWrapper<UserBaseinfo>(userBaseinfoMapper).eq(UserBaseinfo::getUserId, userId).one();



        return new ResultVO(true,"success",userBaseinfo);
    }

}
